package com.heer.gymnasium.adapter;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heer.gymnasium.R;
import com.heer.gymnasium.constant.Type;
import com.heer.model.SitePlaceInfoBean;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by heer on 17/4/11.
 */

public class SiteDetailsTypeAdapter extends ListAdapter<SitePlaceInfoBean.Ticket> {
    private int count;

    public SiteDetailsTypeAdapter(Context context) {
        super(context);
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = mInflater.inflate(R.layout.item_sites_type, parent, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        SitePlaceInfoBean.Ticket item = getItem(position);

        switch (item.getCat_id()) {

            case Type.SITE_TICKET_TYPE_1://1:门票
                holder.layoutType1.setVisibility(View.VISIBLE);
                holder.layoutType2.setVisibility(View.GONE);
                holder.tvName.setText(item.getTicket_name());
                holder.tvPrice.setText(item.getPrice_now());
                holder.tvType.setText("门");
                break;
            case Type.SITE_TICKET_TYPE_2://2:次卡
                holder.layoutType1.setVisibility(View.VISIBLE);
                holder.layoutType2.setVisibility(View.GONE);
                holder.tvName.setText(item.getTicket_name());
                holder.tvPrice.setText(item.getPrice_now());
                holder.tvType.setText("次");
                break;
            case Type.SITE_TICKET_TYPE_3://3:期卡
                holder.layoutType1.setVisibility(View.VISIBLE);
                holder.layoutType2.setVisibility(View.GONE);
                holder.tvName.setText(item.getTicket_name());
                holder.tvPrice.setText(item.getPrice_now());
                holder.tvType.setText("期");
                break;
            case Type.SITE_TICKET_TYPE_4://4:包场
                holder.layoutType1.setVisibility(View.GONE);
                holder.layoutType2.setVisibility(View.VISIBLE);
                holder.tvTime.setText(item.getOne_time());
                SpannableString spannableString = new SpannableString(item.getNum() + "场可预定");
                if (TextUtils.equals(item.getNum(), "0")) {
                    spannableString.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.colorTextLight)), 0, item.getNum().length(),
                            //setSpan时需要指定的 flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括).
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                } else {
                    spannableString.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.colorOrange)), 0, item.getNum().length(),
                            //setSpan时需要指定的 flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前后都不包括).
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                holder.tvNum.setText(spannableString);
                break;
        }

        return view;
    }

    public void setCount(int count) {
        this.count = count;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return count;
    }

    static class ViewHolder {
        @BindView(R.id.image_ticket)
        ImageView imageTicket;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.layout_type1)
        LinearLayout layoutType1;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_num)
        TextView tvNum;
        @BindView(R.id.tv_type)
        TextView tvType;
        @BindView(R.id.tv_type1)
        TextView tvType1;
        @BindView(R.id.layout_type2)
        LinearLayout layoutType2;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
